//
//  YBLearnerViewController.m
//  ZhongWangYuanApp
//
//  Created by apple on 18/6/3.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YBLearnerViewController.h"
#import "YBObjectIntroductionModel.h"
#import "YBLearnerDetailTableViewCell.h"

#define learnerCell @"YBLearnerDetailTableViewCell"

@interface YBLearnerViewController ()<UITableViewDataSource , UITableViewDelegate>
@property (nonatomic , strong) YBObjectIntroductionModel * myModel;

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) NSMutableArray * dataArray;

@property (nonatomic , strong) UIButton * functionButton;

@end


@implementation YBLearnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.myModel = (YBObjectIntroductionModel *)self.model;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(option_objectClassProsessIsSignIn) name:@"ObjectClassProsessIsSignIn" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(option_objectClassProsessIsSignIn) name:@"ObjectClassDetailIsSignIn" object:nil];
    [self initUserInterface];
    [self reloadDetailInfo];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)option_objectClassProsessIsSignIn{
    self.functionButton.hidden = YES;
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.view , 0)
    .bottomSpaceToView(self.view , 0);
}


-(void)initUserInterface{
    UIView * topBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 5)];
    topBackView.backgroundColor = [UIColor themeGrayColor];
    self.tableView.tableHeaderView = topBackView;
    
    UIView * bottomBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], ItemHeight * 20)];
    bottomBackView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = bottomBackView;
    
    [self.view addSubview:self.tableView];
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.view , 0)
    .bottomSpaceToView(self.view , 0);
    
    [self.view addSubview:self.functionButton];
    self.functionButton.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .bottomSpaceToView(self.view , 0)
    .heightIs(ItemHeight * 90);
}




#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.dataArray[indexPath.row][@"height"] floatValue] + 74 * ItemHeight;
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YBLearnerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:learnerCell forIndexPath:indexPath];
    [cell setDataWithDic:self.dataArray[indexPath.row]];
    return cell;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}



-(void)reloadDetailInfo{
    [self.dataArray removeAllObjects];
    if ([self.myModel.isguidingideology integerValue] == 1 && self.myModel.guidingideology.length > 0) {
        [self.dataArray addObject:[self calculateDataWithTitle:@"指导思想" detail:self.myModel.guidingideology]];
    }
    if ([self.myModel.istargetrequirements integerValue] == 1 && self.myModel.targetrequirements.length > 0) {
        [self.dataArray addObject:[self calculateDataWithTitle:@"教学目标" detail:self.myModel.targetrequirements]];
    }
    if ([self.myModel.isgraduationrequirement integerValue] == 1 && self.myModel.graduationrequirement.length > 0) {
        [self.dataArray addObject:[self calculateDataWithTitle:@"结业要求" detail:self.myModel.graduationrequirement]];
    }
    if ([self.myModel.isteachingarrangement integerValue] == 1 && self.myModel.teachingarrangforapp.length > 0) {
        [self.dataArray addObject:[self calculateDataWithTitle:@"教学安排" detail:self.myModel.teachingarrangforapp]];
    }
    [self.tableView reloadData];
    
    if ([self.myModel.issignin isEqualToString:@"1"]) {
        self.functionButton.hidden = YES;
        self.tableView.sd_layout
        .leftSpaceToView(self.view , 0)
        .rightSpaceToView(self.view , 0)
        .topSpaceToView(self.view , 0)
        .bottomSpaceToView(self.view , 0);
    }
    else{
        self.functionButton.hidden = NO;
        self.tableView.sd_layout
        .leftSpaceToView(self.view , 0)
        .rightSpaceToView(self.view , 0)
        .topSpaceToView(self.view , 0)
        .bottomSpaceToView(self.view , 90 * ItemHeight);
    }
}


-(NSDictionary *)calculateDataWithTitle:(NSString *)title detail:(NSString *)detail{
    detail = [NSString stringWithFormat:@"尚锟%@" , detail];
    if (detail.length == 0) {
        detail = @" ";
    }
    NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.lineBreakMode = NSLineBreakByCharWrapping;
    paragraph.lineSpacing = ItemHeight * 20;
    CGSize textSize = [detail boundingRectWithSize:CGSizeMake([UIScreen zj_screenWith] - ItemWidth * 40, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(14) , NSParagraphStyleAttributeName : paragraph} context:nil].size;
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:detail attributes:@{NSFontAttributeName : LYPFONT(14) , NSParagraphStyleAttributeName : paragraph}];
    [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, 2)];
    return @{@"title":title , @"height":@(textSize.height + 1) , @"text":[attributeString copy]};
}


-(void)option_functionButton:(UIButton *)sender{
    [YJProgressHUD show:@"报名中..." inView:[UIApplication sharedApplication].keyWindow mode:YJProgressModeLoading];
    [YBRequestManager request_objectSignInWithIdString:self.myModel.ID successBlock:^(id object) {
        [YJProgressHUD hide];
        if ([object[@"code"] integerValue] == 1) {
            if ([object[@"data"][@"result"] integerValue] == 1) {
                [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"currentIsSign"];
                [YJProgressHUD showMsgWithoutView:@"报名成功"];
                self.functionButton.hidden = YES;
                self.tableView.sd_layout
                .leftSpaceToView(self.view , 0)
                .rightSpaceToView(self.view , 0)
                .topSpaceToView(self.view , 0)
                .bottomSpaceToView(self.view , 0);
                [self.tableView updateLayout];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ObjectClassLearnerIsSignIn" object:nil];
            }
            else{
                [YJProgressHUD showMsgWithoutView:[NSString stringWithFormat:@"%@",object[@"data"][@"message"]]];
            }
        }
        else{
            [YJProgressHUD showMsgWithoutView:@"报名失败"];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"报名失败"];
    }];
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - LP_NavViewHeight - 465 * ItemHeight - 50 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[YBLearnerDetailTableViewCell class] forCellReuseIdentifier:learnerCell];
        
    }
    return _tableView;
}

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


-(UIButton *)functionButton
{
    if (!_functionButton) {
        _functionButton = [ToolClass createButtonWithFrame:CGRectMake(0, 0, 1, 1) text:@"报名" textColor:[UIColor whiteColor] imageName:@"" font:16];
        _functionButton.backgroundColor = [UIColor themeColor];
        [_functionButton setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
        _functionButton.hidden = YES;
        [_functionButton addTarget:self action:@selector(option_functionButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _functionButton;
}


@end
